package tuhao.hibernate.www.common.util;

/**
 * 工具类-图片处理
 */
public class ImageUtil {

	/*private static final Logger LOGGER= Logger.getLogger(ImageUtil.class);
	private static FileInputStream fis;

	*//**
	 * 校验文件是否是图片，是:true，否:false
	 * 
	 * @param file 需要验证的File文件
	 * @return true or false
	 *//*
	@SuppressWarnings("resource")
	public static boolean fileIsImage(File file) {
		InputStream is = null;
		BufferedReader reader = null;
		try {
			// 将文件转换成输入流
			is = new FileInputStream(file);
			// 用image IO读取文件，如果文件file不是图片，则为null
			BufferedImage image = ImageIO.read(is);
			if (image != null) { // 如果image不为空，则说明file文件是图片
				reader = new BufferedReader(new FileReader(file));
				String exits = null;
				while ((exits = reader.readLine()) != null) {
					exits = shiftD(exits);
					if (exits.indexOf("eval(") > 0 || exits.indexOf("<?php") > 0 || exits.indexOf("eval") > 0) {
						return false;
					}
				}
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (is != null) {
					is.close();
				}
				if (reader != null) {
					reader.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	*//**
	 * 
	 * @param str
	 * @return
	 *//*
	public static String shiftD(String str) {
		int size = str.length();
		char[] chs = str.toCharArray();
		for (int i = 0; i < size; i++) {
			if (chs[i] <= 'Z' && chs[i] >= 'A') {
				chs[i] = (char) (chs[i] + 32);
			}
		}
		return new String(chs);
	}


	*//**
	 * 防止处理图片变色
	 * 
	 * @param image
	 * @return
	 *//*
	public static BufferedImage toBufferedImage(Image image) {
		if (image instanceof BufferedImage) {
			return (BufferedImage) image;
		}

		image = new ImageIcon(image).getImage();
		BufferedImage bimage = null;
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		try {
			int transparency = Transparency.OPAQUE;
			GraphicsDevice gs = ge.getDefaultScreenDevice();
			GraphicsConfiguration gc = gs.getDefaultConfiguration();
			bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
		} catch (HeadlessException e) {// linux没有专门处理图片的图形界面,但是不会影响正常的图片处理。此处不用补货异常
		}

		if (bimage == null) {
			int type = BufferedImage.TYPE_INT_RGB;
			bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
		}

		Graphics g = bimage.createGraphics();
		g.drawImage(image, 0, 0, null);
		g.dispose();
		return bimage;
	}

	*//**
	 * 添加图片水印,添加完成之后，默认覆盖原有图片，另，在对图片处理时，要防止图片变色
	 * 
	 * @param targetImg 目标图片路径
	 * @param waterImg 水印图片路径
	 * @param x 水印图片距离目标图片左侧的偏移量，如果x<0, 则在正中间
	 * @param y 水印图片距离目标图片上侧的偏移量，如果y<0, 则在正中间
	 * @param alpha 透明度(0.0 -- 1.0, 0.0为完全透明，1.0为完全不透明)
	 *//*
	public final static void pressImage(String targetImg, String waterImg, String imageType, int x, int y, float alpha) {
		try {
			File file = new File(targetImg);
			Image image = ImageIO.read(file);
			int width = image.getWidth(null);
			int height = image.getHeight(null);
			BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
			Graphics2D g = bufferedImage.createGraphics();
			g.drawImage(image, 0, 0, width, height, null);

			Image waterImage = ImageIO.read(new File(waterImg)); // 水印文件
			int width_1 = waterImage.getWidth(null);
			int height_1 = waterImage.getHeight(null);
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));

			int widthDiff = width - width_1;
			int heightDiff = height - height_1;
			if (x < 0) {
				x = widthDiff / 2;
			} else if (x > widthDiff) {
				x = widthDiff;
			}
			if (y < 0) {
				y = heightDiff / 2;
			} else if (y > heightDiff) {
				y = heightDiff;
			}
			g.drawImage(waterImage, x, y, width_1, height_1, null); // 水印文件结束
			g.dispose();
			ImageIO.write(bufferedImage, imageType, file);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	*//**
	 * @param source
	 * @param dest
	 * @param quality 图片质量,最好不要设置为100 。 0<quality<100 图片格式转换
	 *//*
	public static void toJpg(String source, int quality) {
		// 0<quality<100
		if (quality < 0 || quality > 100 || (quality + "") == null || (quality + "").equals("")) {// 图片质量,如果为空，则默认为75
			quality = 75;
		}

		String outfile = getFileName(source) + getFileTypeOne(source);
		try {
			JPGOptions options = new JPGOptions();
			options.setQuality(quality);
			ImageProducer image = Jimi.getImageProducer(source);
			JimiWriter writer = Jimi.createJimiWriter(outfile);
			writer.setSource(image);
			// 加入属性设置，非必要
			// *//*
			writer.setOptions(options);
			// *//*
			writer.putImage(outfile);
		} catch (Exception je) {
			je.printStackTrace();
		}
	}

	*//**
	 * 上传文件名称
	 * 
	 * @param file
	 * @return
	 *//*
	public static String getFileName(String file) {
		return file.substring(0, file.lastIndexOf("."));
	}

	*//**
	 * 提取上传文件类型名。如23424234.gif，返回".gif"
	 * 
	 * @param file
	 * @return
	 *//*
	public static String getFileTypeOne(String file) {
		return file.substring(file.lastIndexOf("."));
	}

	*//**
	 * 提取上传文件类型名。如23424234.gif，返回"gif"
	 * 
	 * @param file
	 * @return
	 *//*
	public static String getFileTypeTwo(String file) {
		return file.substring(file.lastIndexOf(".") + 1);
	}
	
	*//**
	 * 获取文件大小
	 * @param filePath
	 *//*
	public static int getFileByte(File file){
        try{   
            fis = new FileInputStream(file);   
            try{    
            	return fis.available();   
            }catch(IOException e1){   
            	e1.printStackTrace();   
            }   
         }catch(FileNotFoundException e2){   
        	 e2.printStackTrace();   
         }   
        return 0;
    }*/
}
